#include "ContinousDisplay.h"

//#include "ContinousDisplay.h"
//#include "DisplayController.h"
#include "History.h"
#include <stdio.h>

#include <QPainter>

#include "Signal.h"

ContinousDisplay::ContinousDisplay(Signal* s, QColor color, int height, bool positionIsShared)
	: LMONDisplay(s, color, height, positionIsShared), _displayOffset(0), _scaleX(1)
{

}

void ContinousDisplay::drawSignal(QPaintDevice* pPD)
{
	QPainter painter(pPD);

	int firstIndex;
	int valueCount = _rect.width();

	vector<Reading> v = _pHistory->tail(valueCount, _scaleX, firstIndex);
	
	for(signed int i = 1; i<v.size(); i++)
	{
		painter.setPen(_color);
		painter.drawLine(QPoint(i, scaleY(v[i-1].value)/* + _displayOffset*/),
			 QPoint(i, scaleY(v[i].value) /*+ _displayOffset*/));
//		painter.drawRect(_rect);
	}

	// Moving X-axis
	for(unsigned int x = 10 - (firstIndex%10), i = firstIndex + x;
		x+10*_scaleX<=_rect.width(); i+=10, x+=10*_scaleX)
	{
		painter.drawLine(x, _rect.bottom()-7, x, _rect.bottom());
	}

	// Static x axis
/*	for(signed int x = 0; x+10*_scaleX < _rect.width(); x+=10*_scaleX)
	{
		painter.drawLine(x, _rect.height()-7, x, _rect.height()-2);
	}*/
}

int ContinousDisplay::scaleY(int y)
{
	int h = _rect.height();
	return h-y*h/2096/*-h/2*/+_rect.top();
}

void ContinousDisplay::setScaleX(int scale)
{
	_scaleX = scale;
}
